from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart,MIMEBase
from email.header import Header
from email import encoders
from email.utils import parseaddr,formataddr

import smtplib

def _format_addr(s):
    name,addr=parseaddr(s)
    return formataddr((Header(name,'utf-8').encode(),addr))


from_addr='15906502705@163.com'
password='Lrr913910'
to_addr='Liao Ruru@mail.zhonghua-paper.com'
smtp_server='smtp.163.com'

msg=MIMEMultipart()#subtype=alternative ,commix HTML and Plain

msgtext=MIMEText('hello ,send by python.It is something about test.And please ','plain','utf-8')
msgtexthtml=MIMEText('<html><body><h1>Hello</h1>'+
                     '<p>send by <a href="hettp://www.python.org">Python</a>...</p>'+
                     '</body></html>',
                     'html','utf-8')#HTML MIMEText

msg['From']=_format_addr('Ru<%s>'%from_addr)
msg['To']=_format_addr(to_addr)
msg['Subject']=Header('Hello ,from SMTP PY...','utf-8').encode()

msg.attach(msgtext)
with open(r'F:\backuppy\xjbh.gif','rb')as f:
    mime=MIMEBase('image','gif',filename='xjbh.gif')
    mime.add_header('Content-Disposition','attachment',filename='xjbh.gif')
    mime.add_header('Content_ID','<0>')
    mime.add_header('X-Attachment-Id','0')
    
    mime.set_payload(f.read())
    encoders.encode_base64(mime)
    msg.attach(mime)
    

server=smtplib.SMTP(smtp_server,25)
server.starttls()
server.set_debuglevel(1)
server.login(from_addr,password)
server.sendmail(from_addr,[to_addr],msg.as_string())
server.quit()

